package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * 角色控制层
 */
@RestController
@RequestMapping("/roleInfo")
public class RoleController {
    @Reference
    private RoleService roleService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping(value = "/findPage", method = RequestMethod.POST)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = roleService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }

    /**
     * 新增角色
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody Role role) {
        try {
            roleService.add(role);
            return new Result(true, MessageConstant.INSERT_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.INSERT_ROLE_FAIL);
        }
    }

    /**
     * 查询角色
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Result findById(Integer roleId) {
        try {
            Role role1 = roleService.findById(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 修改角色
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    public Result edit(@RequestBody Role role) {
        try {
            //修改角色
            roleService.edit(role);
            return new Result(true, MessageConstant.UPDATE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.UPDATE_ROLE_FAIL);
        }
    }

    /**
     * 根据角色id 查询菜单id
     */
    @RequestMapping(value = "findRoleMenuIdsByRoleId", method = RequestMethod.GET)
    public List<Integer> findRoleMenuIdsByRoleId(Integer roleId) {
        return roleService.findRoleMenuIdsByRoleId(roleId);
    }

    /**
     * 根据角色id 查询权限id
     */
    @RequestMapping(value = "findRolepermissionIdsByRoleId", method = RequestMethod.GET)
    public List<Integer> findRolepermissionIdsByRoleId(Integer roleId) {
        return roleService.findRolepermissionIdsByRoleId(roleId);
    }

    /**
     * 根据角色id，删除角色
     */
    @RequestMapping(value = "deleteById", method = RequestMethod.GET)
    public Result deleteById(Integer roleId) {
        try {
            roleService.deleteById(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    /**
     * 查询所有权限
     */
    @RequestMapping(value = "queryAllPermissions", method = RequestMethod.GET)
    public Result queryAllPermissions() {
        try {
            List<Permission> list = roleService.queryAllPermissions();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 查询所有菜单
     */
    @RequestMapping(value = "findAllMenu", method = RequestMethod.GET)
    public Result findAllMenu() {
        try {
            List<Menu> list = roleService.findAllMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 根据角色id修改角色和菜单表的关联
     */
    @RequestMapping(value = "menupermissionedit", method = RequestMethod.POST)
    public Result menupermissionedit(@RequestBody Role role, Integer[] menu, Integer[] permission) {
        try {
            Integer roleId = role.getId();
            jedisPool.getResource().del("menu".getBytes());
            roleService.menupermissionedit(roleId, menu, permission);
            return new Result(true, "关联菜单修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, "关联菜单修改失败");
        }
    }
}
